package com.sky.robot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.robot.entity.SkyTable;
import com.sky.robot.domain.SkyTableDo;
import com.sky.robot.service.SkyTableService;
import com.sky.robot.utils.Result;
import com.sky.robot.utils.SkyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 通过读取table、column数据进行驱动建表
 */
@RestController
@RequestMapping("/core/skyTable/")
public class SkyTableController {

    @Autowired
    private SkyTableService skyTableService;

    /**
     * 前端页面传过来第几页，每一页数据数量
     */
    @PostMapping("/listPage")
    public Object listPage(@RequestBody Map<String, Object> params) {
        Integer pageNum = Integer.parseInt((String) params.get("pageNum")); // 获取第几页
        Integer pageSize = Integer.parseInt((String) params.get("pageSize")); // 每页多少条数据
        IPage<SkyTable> page = new SkyPage<>(pageNum, pageSize);
        Object obj = skyTableService.listPage(page, params);
        return Result.success(obj);
    }

    @PostMapping("/get")
    public Object get(@RequestBody Map<String, Object> params) {
        Object obj = skyTableService.get(params);
        return obj;
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody SkyTable skyTable) {
        Object obj = skyTableService.add(skyTable);
        return Result.success(obj);
    }

    /**
     * 修改
     */
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody SkyTable skyTable) {
        Object obj = skyTableService.edit(skyTable);
        return Result.success(obj);
    }

    /**
     * 通过id删除
     */

    @DeleteMapping("/delete")
    public Result<?> delete(String id) {
        skyTableService.delete(id);
        return Result.success(id);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        skyTableService.deleteBatch(ids);
        return Result.success(ids);
    }

    @PostMapping("/createTable")
    public Result<?> createTable(@RequestBody SkyTableDo skyTableDo) {

        Object obj = skyTableService.createTable(skyTableDo);

        return Result.success(obj);
    }

    @GetMapping("/generatorTable")
    public Result generatorTable(String tableName) {
        skyTableService.generatorCode(tableName);
        return Result.success();
    }
}
